<template>
  <div class="avue-contail"
       :class="{'avue--collapse':isCollapse}">
    <div class="avue-header">
      <!-- 顶部导航栏 -->
      <top />
    </div>

    <div class="avue-layout">
      <div class="avue-left">
        <!-- 左侧导航栏 -->
        <sidebar />
      </div>
      <div class="avue-main">
        <!-- 顶部标签卡 -->
        <tags />
        <!-- 主体视图层 -->
        <el-scrollbar style="height:100%">
          <keep-alive>
            <router-view class="avue-view"
                         v-if="$route.meta.$keepAlive" />
          </keep-alive>
          <router-view class="avue-view"
                       v-if="!$route.meta.$keepAlive" />
        </el-scrollbar>

      </div>
    </div>
    <!-- <el-footer class="avue-footer">
      <img src="/svg/logo.svg"
           alt=""
           class="logo">
      <p class="copyright">© 2018 Avue designed by smallwei</p>
    </el-footer> -->
    <div class="avue-shade"
         @click="showCollapse"></div>

    <!--<div class="el-alert el-alert&#45;&#45;info warning">
      <div class="el-alert__content">
        <span class="el-alert__title">
            <span>标题shu    fsfasfasfsfa</span>
        </span>
      </div>
    </div>-->

    <el-card class="box-card warning" v-show="warningHidden">
      <div slot="header" class="clearfix">
        <span>设备预警</span><i class="el-icon-close" @click="warningHidden=false"></i>
      </div>
      <div class="text item">
        <P class="p1"><i class="el-icon-warning"></i>{{warningData}}</P>
        <p class="p2">请立即前往处理
          <el-button size="mini" @click="dashboardBtn()">查看</el-button></p>
      </div>
    </el-card>
  </div>

</template>

<script>
import { mapGetters } from 'vuex'
import tags from './tags'
import top from './top/'
import sidebar from './sidebar/'
import admin from '@/util/admin';
import { validatenull } from '@/util/validate';
import { calcDate } from '@/util/date.js';
import { getStore } from '@/util/store.js';
import website from "@/const/website";
var ws;
export default {
  components: {
    top,
    tags,
    sidebar
  },
  name: 'index',
  data () {
    return {
      warningHidden:false,
      warningData:0,
      //刷新token锁
      refreshLock: false,
      //刷新token的时间
      refreshTime: '',
      // website:website,
      userInfoX:getStore({ name: "userInfoData" }),
    }
  },
  created () {
    //实时检测刷新token
    // this.refreshToken();
  },
  mounted () {
    this.init();
    this.initSocket()
  },
  computed: mapGetters(['isLock', 'isCollapse', 'website']),
  props: [],
  methods: {
      initSocket() {
          var url = this.website.axiosUrl + '/websocket/'+this.userInfoX.id+'/'+this.userInfoX.username;
          let reg=new RegExp('http','g')//g代表全部
          url = JSON.stringify(url).replace(reg,'ws');
          url = url.substr(1,url.length-2)
          console.log(url)
          if ('WebSocket' in window) {
              ws = new WebSocket(url); // 实例化socket
          } else if ('MozWebSocket' in window) {
              ws = new MozWebSocket(url); // 实例化socket
          } else {
              this.ME("该浏览器不支持websocket")
              return;
          }
          // 监听socket连接
          ws.onopen = this.open
          // 监听socket错误信息
          ws.onerror = this.error
          // 监听socket消息
          ws.onmessage = this.getMessage
          // 监听关闭
          ws.onclose = this.close
      },
      open: function () {
          console.log('链接成功')
      },
      error: function () {
      },
      getMessage: function (msg) {
          this.warningData = msg.data
          this.warningHidden = (validatenull(msg.data)|| msg.data == '连接成功') ? false:true;
      },
      send: function () {
          this.socket.send(params)
      },
      close: function () {
      },
    dashboardBtn(){
        // 新增维保执行-跳转到维保执行（已维保）
        if (this.warningData.indexOf('维保执行') > -1) {
          this.$router.push({ path: '/aintenance/maintenanceExecution', query: { tabItem: '1' } });
        } else {
          this.$router.push({ path: '/aintenance/dashboard' });
        }
        this.warningHidden = false
    },
    showCollapse () {
      this.$store.commit("SET_COLLAPSE");
    },
    // 屏幕检测
    init () {
      this.$store.commit('SET_SCREEN', admin.getScreen());
      window.onresize = () => {
        setTimeout(() => {
          this.$store.commit('SET_SCREEN', admin.getScreen());
        }, 0);
      }
    },
    // 实时检测刷新token
    refreshToken () {
      this.refreshTime = setInterval(() => {
        const token = getStore({
          name: 'token',
          debug: true,
        });
        const date = calcDate(token.datetime, new Date().getTime());
        if (validatenull(date)) return;
        if (!(date.seconds >= this.website.tokenTime) && !this.refreshLock) {
          this.refreshLock = true;
          this.$store
            .dispatch('RefeshToken')
            .then(() => {
              clearInterval(this.refreshTime);
            })
            .catch(() => {
              this.refreshLock = false;
            });
        }
      }, 3000);
    },
  }
}
</script>
<style>
  .warning{
    z-index: 99999;
    position: fixed!important;
    bottom: 5px!important;
    right: 5px!important;
    width: 200px!important;
    height: 45px!important;
  }
  .warning .p1{
    font-size: 15px;
    vertical-align: middle;
    display: flex;
    }
  .warning .p2{
    font-size: 13px;
    float: right;
    margin: 10px 0px;
  }
  .warning .p2 .el-button--mini{
    padding:5px 10px!important;
  }
  .warning .el-icon-warning{
    font-size: 27px;
    color: red;
    margin-top: -2px;
  }
  .warning .el-icon-close{
    float: right;
   }
</style>